//
//  PATradeInfoCell.m
//  PAMobileWallet
//
//  Created by Gavin Zhang on 7/14/13.
//  Copyright (c) 2013 平安付. All rights reserved.
//

#import "PATradeInfoCell.h"
#import "PAAsynImageView.h"
#import "PATradeInfo.h"
#import "UIImage+Extend.h"
#import <QuartzCore/QuartzCore.h>
#define HeadImageViewWidth 30
#define HeadImageViewHeight 30
#define BackgroundImageViewHeight 40
#define TradeStatusImageViewWidth 15
#define TradeStatusImageViewHeight 15
#define VoiceCommentButtonWidth 30
#define VoiceCommentButtonHeight 30

@interface PATradeInfoCell ()
@property (nonatomic, weak)UIViewController *parentController;
@property (nonatomic, strong)UIImageView *cycleIV;
@end

@implementation PATradeInfoCell
@synthesize headIV = _headIV;
@synthesize backgroundIV = _backgroundIV;
@synthesize amountLAB = _amountLAB;
@synthesize tradeStatusIV = _tradeStatusIV;
@synthesize voiceCommentBTN = _voiceCommentBTN;
@synthesize statusBgIV      = _statusBgIV;
@synthesize statusLAB       = _statusLAB;
@synthesize parentController = _parentController;
@synthesize cycleIV         = _cycleIV;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.cycleIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, HeadImageViewWidth+8, HeadImageViewHeight+8)];
        self.cycleIV.image = [UIImage imageNamed:@"contact_face_non.png"];
        [self.contentView addSubview:self.cycleIV];
        
        self.headIV = [[PAAsynImageView alloc] initWithFrame:CGRectMake(4, 4, HeadImageViewWidth, HeadImageViewHeight)];
        self.headIV.defaultImg = [UIImage imageNamed:@"pay_addContact.png"];
        self.headIV.cornerRadius = HeadImageViewHeight/2;
        self.headIV.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.headIV];
        
        self.backgroundIV = [[UIImageView alloc] initWithFrame:CGRectMake(HeadImageViewWidth+1, 2, 200, BackgroundImageViewHeight)];
        self.backgroundIV.contentMode = UIViewContentModeScaleToFill;
        [self.contentView addSubview:self.backgroundIV];
        
        self.amountLAB = [[UILabel alloc] initWithFrame:CGRectMake(0, 7, 100, 20)];
        self.amountLAB.backgroundColor = [UIColor clearColor];
        [self.amountLAB setTextAlignment:NSTextAlignmentCenter];
        [self.amountLAB setFont:[UIFont systemFontOfSize:15]];
        [self.backgroundIV addSubview:self.amountLAB];
        
        self.voiceCommentBTN  = [[PAAsynMedialButton alloc] initWithFrame:CGRectMake(150, 6, VoiceCommentButtonWidth, VoiceCommentButtonHeight)];
        [self.voiceCommentBTN setImage:[UIImage imageNamed:@"pay_voiceShow.png"] forState:UIControlStateNormal];
        [self.voiceCommentBTN setImage:[UIImage imageNamed:@"pay_voiceShow.png"] forState:UIControlStateHighlighted];
        [self.voiceCommentBTN addTarget:self action:@selector(playVoice) forControlEvents:UIControlEventTouchUpInside];
        self.voiceCommentBTN.userInteractionEnabled = YES;
        [self.contentView addSubview:self.voiceCommentBTN];
        
        self.statusBgIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, BackgroundImageViewHeight+2, 95, 20)];
        self.statusBgIV.image = [UIImage imageNamed:@"pay_situation_bg.png"];
        [self.contentView addSubview:self.statusBgIV];
        
        
        self.tradeStatusIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, BackgroundImageViewHeight+5, TradeStatusImageViewWidth, TradeStatusImageViewHeight)];
        [self.contentView addSubview:self.tradeStatusIV];
        self.tradeStatusIV.hidden = YES;
        
        self.statusLAB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 95, 20)];
        [self.statusLAB setTextAlignment:NSTextAlignmentCenter];
        [self.statusLAB setFont:[UIFont systemFontOfSize:12]];
        [self.statusLAB setTextColor:[UIColor whiteColor]];
        [self.statusLAB setBackgroundColor:[UIColor clearColor]];
        [self.statusBgIV addSubview:self.statusLAB];
        
        UITapGestureRecognizer* singleRecognizer;
        singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom:)];
        [self.voiceCommentBTN addGestureRecognizer:singleRecognizer];
    }
    return self;
}

- (void)handleSingleTapFrom:(UITapGestureRecognizer*)recognizer
{
    [self playVoice];
}

- (void)loadInfo:(PATradeInfo *)info
{
    int tradeDirection = info.tradeDirection;
    CGRect rect;
    if (tradeDirection != TDT_Out){
        self.headIV.hidden = NO;
        self.cycleIV.hidden = NO;
        rect = self.headIV.frame;
        rect.origin.x = 4;
        rect.origin.y = 3;
        self.headIV.frame = rect;
        self.headIV.layer.cornerRadius = rect.size.height/2.0;
        NSString *amountStr = [NSString stringWithFormat:@"  + %.2f  元",info.tradeAmount];
        CGSize size = [amountStr sizeWithFont:[UIFont systemFontOfSize:15]];
        rect = self.backgroundIV.frame;
        self.backgroundIV.image = [[UIImage imageNamed:@"pay_msg1.png"] stretchableImageWithLeftCapWidth:41/2-1 topCapHeight:36];
        rect.size.width = size.width;
        rect.origin.x = self.headIV.frame.origin.x +self.headIV.frame.size.width;
        rect.size.width += 30;
        self.backgroundIV.frame = rect;
        
        rect = self.statusBgIV.frame;
        rect.origin.x = self.headIV.frame.origin.x;
        self.statusBgIV.frame = rect;
        
        self.amountLAB.text = amountStr;
        
        self.tradeStatusIV.image = [UIImage imageNamed:@"register_seclectYES.png"];
        rect = self.tradeStatusIV.frame;
        rect.origin.x = self.backgroundIV.frame.origin.x + self.backgroundIV.frame.size.width + 2;
        self.tradeStatusIV.frame = rect;
        
        rect = self.voiceCommentBTN.frame;
        rect.origin.x = self.tradeStatusIV.frame.origin.x+self.tradeStatusIV.frame.size.width+2;
        [self.voiceCommentBTN setTransform: CGAffineTransformMakeRotation(0)];
        self.voiceCommentBTN.frame = rect;
        
        rect = self.tradeStatusIV.frame;
        rect.origin.x = self.statusBgIV.frame.origin.x+self.statusBgIV.frame.size.width+2;
        self.tradeStatusIV.frame = rect;

    }else{
        self.headIV.hidden = YES;
        self.cycleIV.hidden = YES;
        CGRect rect;
   
        
        NSString *amountStr = [NSString stringWithFormat:@"  - %.2f  元",info.tradeAmount];
        CGSize size = [amountStr sizeWithFont:[UIFont systemFontOfSize:15]];
        rect = self.backgroundIV.frame;
        self.backgroundIV.image = [[UIImage imageNamed:@"pay_msg.png"] stretchableImageWithLeftCapWidth:41/2-1 topCapHeight:36];
        rect.size.width = size.width + 30;
        rect.origin.x = self.contentView.frame.size.width - rect.size.width;
        self.backgroundIV.frame = rect;
        self.amountLAB.text = amountStr;
        
        rect = self.tradeStatusIV.frame;
        rect.origin.x = self.frame.size.width - self.tradeStatusIV.frame.size.width - 5;
        self.tradeStatusIV.frame = rect;
        
        rect = self.statusBgIV.frame;
        rect.origin.x = self.tradeStatusIV.frame.origin.x - rect.size.width - 5;
        self.statusBgIV.frame = rect;
        
        
        rect = self.voiceCommentBTN.frame;
        rect.origin.x = self.backgroundIV.frame.origin.x - rect.size.width - 2;
        
        [self.voiceCommentBTN setTransform: CGAffineTransformMakeRotation(M_PI)];
        self.voiceCommentBTN.frame = rect;
    }
    
    rect = self.amountLAB.frame;
    rect.size.width = self.backgroundIV.frame.size.width;
    self.amountLAB.frame = rect;
    switch (info.tradeStatus) {
        case TST_Failed:{
            self.tradeStatusIV.image = [UIImage imageNamed:@"小失败.png"];
            self.statusLAB.text = @"交易关闭";
        }
            break;
        case TST_Success:{
            self.tradeStatusIV.image = [UIImage imageNamed:@"小成功.png"];
            self.statusLAB.text = @"交易成功";
        }
            break;
        case TST_WaitAccept:{
            self.tradeStatusIV.image = [UIImage imageNamed:@""];
            self.statusLAB.text = @"等待对方收款";
        }
            break;
        case TST_Sending:{
            self.tradeStatusIV.image = [UIImage imageNamed:@""];
            self.statusLAB.text = @"正在发送中";
        }
            break;
        default:{
            self.tradeStatusIV.image = [UIImage imageNamed:@""];
            self.statusLAB.text = @"";
        }
            break;
    }
    self.voiceCommentBTN.mediaUrl = info.mediaUrl;
    self.voiceCommentBTN.hidden = (info.mediaUrl == nil);
    self.voiceCommentBTN.uniqueId = info.tradeId;
    self.voiceCommentBTN.tradeDirection = tradeDirection;
    [self.voiceCommentBTN showTrackTimeIfPossible:tradeDirection];
    if (nil ==self.headIV.imgUrl) {
        self.cycleIV.hidden = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)playVoice
{
    if (self.parentController && [self.parentController respondsToSelector:@selector(playVoiceAction:)]) {
        [self.parentController performSelector:@selector(playVoiceAction:) withObject:self];
    }
}



@end
